﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.ImpUC;
using DXApplicationTangche.UC.套号管理.套号下单;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.销售记录.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.抖音出入库
{
    public partial class Form抖音款式添加 : DevExpress.XtraEditors.XtraForm
    {
        private List<DouyinMaterialDTO> douyinMaterialDTOs = new List<DouyinMaterialDTO>();
        private XtraUserControl订单一览 xtraUserControl订单一览;
        private List<DouyinMTSOrderDTO> douyinMTSOrderDTOs下单记录 = new List<DouyinMTSOrderDTO>();
        //下单orderCommitDTO
        private List<OrderCommitDTO返回> orderCommitDTOs = new List<OrderCommitDTO返回>();

        public Form抖音款式添加()
        {
            InitializeComponent();
            this.刷新();
            this.订单一览刷新();
        }

        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox扫码.Text))
                {
                    MessageBox.Show("条码不能为空");
                    this.textBox扫码.Clear();
                    return;
                }
                //判断成衣码是否正确
                if (!货号管理Service.barcodeExisted(this.textBox扫码.Text.Trim()))
                {
                    MessageBox.Show("成衣码不存在");
                    this.textBox扫码.Clear();
                    return;
                }
                OrderCommitDTO返回 orderCommitDTO =
                                订单预览新Service.getOrderCommitDTOByBarcode(this.textBox扫码.Text.Trim());
                try { orderCommitDTO.styleSizeCd = orderCommitDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                //若尺码为空，默认M
                if (String.IsNullOrEmpty(orderCommitDTO.styleSizeCd))
                {
                    orderCommitDTO.styleSizeCd = "M";
                }
                //若款式为样衣，无refBarcode，取成衣码barcode
                if (String.IsNullOrEmpty(orderCommitDTO.refBarcode))
                {
                    orderCommitDTO.refBarcode = this.textBox扫码.Text.Trim();
                }
                DouyinMaterialDTO douyinMaterialDTO = new DouyinMaterialDTO(orderCommitDTO.refBarcode, orderCommitDTO.styleNameCn, orderCommitDTO.materialCode, orderCommitDTO.styleSizeCd);
                if (this.douyinMaterialDTOs.Count > 0)
                {
                    抖音Service.saveDouyinMaterial(douyinMaterialDTO);
                    this.刷新();
                    this.textBox扫码.Clear();
                    ////抖音款式不存在，添加款式
                    //if ((!this.douyinMaterialDTOs.Exists(dto => dto.styleNameCn.Equals(douyinMaterialDTO.styleNameCn)))
                    //    || (!this.douyinMaterialDTOs.Exists(dto => dto.materialCode.Equals(douyinMaterialDTO.materialCode))))
                    //{
                    //    抖音Service.saveDouyinMaterial(douyinMaterialDTO);
                    //    this.刷新();
                    //    this.textBox扫码.Clear();
                    //}
                    ////抖音款式已存在，不添加
                    //else
                    //{
                    //    MessageBox.Show("款式已存在，请勿重复创建");
                    //    this.textBox扫码.Clear();
                    //    return;
                    //}
                }
            }
        }
        private void 刷新()
        {
            this.douyinMaterialDTOs = 抖音Service.getDouyinMaterialAll();
            this.gridControl抖音款式.DataSource = this.douyinMaterialDTOs;
            this.gridView抖音款式.RefreshData();
        }
        private void 订单一览刷新()
        {
            this.xtraUserControl订单一览 = new XtraUserControl订单一览("11");
            this.xtraUserControl订单一览.Dock = DockStyle.Fill;
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(this.xtraUserControl订单一览);
        }



        private void dockPanel1_Click(object sender, EventArgs e)
        {
            this.订单一览刷新();
        }

        private void gridView抖音款式_DoubleClick(object sender, EventArgs e)
        {
            DouyinMaterialDTO douyinMaterialDTO = new DouyinMaterialDTO(
                FunctionHelper.GetgridViewValue(this.gridView抖音款式, this.gridView抖音款式.FocusedRowHandle, "refBarcode"),
                FunctionHelper.GetgridViewValue(this.gridView抖音款式, this.gridView抖音款式.FocusedRowHandle, "styleNameCn"),
                FunctionHelper.GetgridViewValue(this.gridView抖音款式, this.gridView抖音款式.FocusedRowHandle, "materialCode"),
                FunctionHelper.GetgridViewValue(this.gridView抖音款式, this.gridView抖音款式.FocusedRowHandle, "styleSizeCd"));
            DouyinMTSOrderDTO douyinMTSOrderDTO = new DouyinMTSOrderDTO(douyinMaterialDTO);
            Form抖音套号下单修改商品 Form抖音套号下单修改商品 = new Form抖音套号下单修改商品(douyinMTSOrderDTO);
            Form抖音套号下单修改商品.ShowDialog();
            if (Form抖音套号下单修改商品.is确认)
            {
                this.orderCommitDTOs.Add(Form抖音套号下单修改商品.model.OrderCommitDTO返回);
                this.刷新一览();
            }
        }
        private void 刷新一览()
        {
            this.gridControl下单记录.DataSource = this.orderCommitDTOs;
            this.gridView下单记录.RefreshData();
        }

        private void simpleButton下单_Click(object sender, EventArgs e)
        {
            DTO销售记录 销售记录 =
                    new DTO销售记录(
                        this.orderCommitDTOs,
                        "11",
                        "春衫抖音"
                        );
            销售记录Service.post销售记录(
                销售记录,
                门店下单.form.Frm订单一览.Enum订单类别.套号订单
            );
            MessageBox.Show("保存成功");
            //下单记录清空
            this.orderCommitDTOs.Clear();
            this.gridView下单记录.RefreshData();
            this.订单一览刷新();
        }

        private void gridView下单记录_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码1"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OrderCommitDTO返回 orderCommitDTO删除 = this.orderCommitDTOs[this.gridView下单记录.FocusedRowHandle];
            this.orderCommitDTOs.Remove(orderCommitDTO删除);
            this.刷新一览();
        }
    }
}
